<?php
//print_r($this->ion_auth->is_active(4));
if(isset($edit)){ ?>
<h3>Edit Task</h3>
<?php } else {?>
<h3>Create Task</h3>
<?php }?>
<?= form_open('user/tasks/create_task');?>
<table width="100%" class="" >
  <tr>
    <td>	
	<span class="text-error">*</span> Project Name:	
	</td>
    <td>
    <div class="span4" >
	<select name="project" id="project">
     <option value="" >-- Select Project -- </option>
    <optgroup label="Opened Projects">
	<?php foreach($projects as $project) { if($project->is_completed == 0){?>
    <option value="<?= $project->id?>"<?php 	
	if(isset($edit)) {
	if($edit['project_id'] == $project->id)
	{
		echo 'selected = "selected"';
		}
	}	
	else {	
	echo set_select('projects',$project->id);
	}?> ><?= $project->name?></option>
    <?php }}?>
	</optgroup>
	<optgroup label="Closed Projects">
	<?php foreach($projects as $project) { if($project->is_completed == 1){?>
    <option value="<?= $project->id?>"<?php 	
	if(isset($edit)) {
	if($edit['project_id'] == $project->id)
	{
		echo 'selected = "selected"';
		}
	}	
	else {	
	echo set_select('projects',$project->id);
	}?> ><?= $project->name?></option>
    <?php }}?>	
		
	</optgroup>
    </select>
	</div>
	<?php if(isset($edit)) {?>
	<div class="span4 offset3">
	<div class="span5" <?php if($this->ion_auth->is_client()){?> style="display:none;" <?php }?>><span class="text-error">*</span> Created by:</div>
	<div class="span2">
	<select name="created_by" <?php if($this->ion_auth->is_client()){?> style="display:none;" <?php }?> >   
    <?php foreach ($users as $user) {
		if(($this->ion_auth->is_active($user->id))){
	?>
    <option value="<?php echo $user->user_id;?>" 
	<?php 
	if(isset($edit)) {
	if($edit['created_by'] == $user->id)
	{
		echo 'selected = "selected"';
		}
	}	
	else {
	echo set_select('user',$user->user_id);
	}
	?> ><?php
	if($this->session->userdata('user_id')==$user->user_id)
		{
		echo 'Me';
		}
		else{
			if($this->ion_auth->is_client($user->id)){
				echo $user->username.' (client)';
			}
			else{
	echo $user->username;			
			}
	
		}
	 ?></option >
    <?php }} ?>
    </select>
	</div>
	</div>
	<?php }?>
    </td>
    <td class="text-error"><?= form_error('project'); ?></td>
  </tr>  
  <tr>
    <td>Milestone</td>
    <td id="milestone"> 
	
	 <select name="milestone">   
    <?php foreach($milestones as $milestone) {?>
    <option value="<?= $milestone->id?>"
     <?php 	echo set_select('milestone',$milestone->id);?> >
	 <?= $milestone->name?>
	 </option>
    <?php }?>
    </select>
   </td>
    <td class="text-error"><?= form_error('taskgroup'); ?></td>
  </tr>
  <tr>
    <td><span class="text-error">*</span> Task Group</td>
    <td id="taskgroup">    
    <select name="taskgroup">    
    <?php foreach($taskgroups as $taskgroup) {?>
    <option value="<?= $taskgroup->id?>" 
    <?php
	if(isset($edit)) {
	if($edit['taskgroup_id'] == $taskgroup->id)
	{
		echo 'selected = "selected"';
		}
	}	
	else {		
	 echo set_select('taskgroup',$taskgroup->id);
	 }
	?>
     ><?= $taskgroup->name?></option>
    <?php }?>
    </select>
    </td>
    <td class="text-error">&nbsp;</td>
  </tr>
  <?php if(!$this->ion_auth->is_client()) {?>
  <tr>
    <td width="10%" height="41"><span class="text-error">*</span> For</td>
    <td width="60%">     
    
	<select name="user">   
    <?php foreach ($users as $user) {
	
	if( !($this->ion_auth->is_client($user->id)) AND ($this->ion_auth->is_active($user->id)) ){		
		?>
    <option value="<?php echo $user->user_id;?>" 
	<?php 
	if(isset($edit)) {
	if($edit['user_id'] == $user->id)
	{
		echo 'selected = "selected"';
		}
	}
	elseif($this->session->userdata('user_id')==$user->user_id)
	{
		echo 'selected = "selected"';
	}
	else {
	echo set_select('user',$user->user_id);
	}
	?> ><?php
	if($this->session->userdata('user_id')==$user->user_id)
		{
		echo 'Me';
		}
		else{
	echo $user->username;
		}
	 ?></option >
    <?php }} ?>
    </select>
	
    <span ></span>
    </td>
    <td width="20%" class="text-error"><?= form_error('user'); ?></td>
  </tr>
  <?php }else {?>
  <input type="hidden" name="user" id="user" />
  <?php }?>
  <tr>
    <td><span class="text-error">*</span> Title</td>
    <td>
    <input type="text" name="title"  value="<?= isset($edit['title']) ? $edit['title'] : set_value('title')?>" class="span12"/></td>
    <td class="text-error"><?= form_error('title'); ?>
    </td>
  </tr>
  <tr>
    <td><span class="text-error">*</span>Description</td>
    <td><textarea name="desc" class="span12"><?= isset($edit['title']) ? $edit['desc'] : set_value('desc')?></textarea></td>
    <td class="text-error"><?= form_error('desc'); ?> </td>
  </tr>
  <tr>
    <td><span class="text-error">*</span> Start Date </td>
    <td><input type="text" name="start_date" id="dpd1"  value="<?= isset($edit['start_date']) ? $edit['start_date'] : set_value('start_date')?>"/>
      <small class="text-error">
      <?= form_error('start_date'); ?>
    </small>
    
    </td>
    <td class="text-error">&nbsp;</td>
  </tr>
  <tr>
    <td><span class="text-error">*</span> Final Due Date</td>
    <td><input type="text" id="dpd2" name="end_date"  value="<?= isset($edit['end_date']) ? $edit['end_date'] : set_value('end_date')?>"/>
      <small class="text-error">
      <?= form_error('end_date'); ?>
    </small>
    
    </td>
    <td class="text-error">&nbsp;</td>
  </tr>
  <tr>
    <td><span class="text-error">*</span> Priority</td>
    <td>   
    <select name="priority" class="chzn-select">  
    <option value="low" <?php 
	if (isset($edit['priority'])) {
	if($edit['priority'] == 'low'){ echo 'selected = "selected"'; } }echo set_select('priority','low');?> 
	 >Low</option>
    <option value="medium"  <?php 
	if (isset($edit['priority'])) 
	{ if($edit['priority'] == 'medium')
	{ echo 'selected = "selected"'; }
	}
	echo set_select('priority','medium');
	?> >Medium</option>
    <option value="high"  <?php 
	if (isset($edit['priority'])) 
	{
	if($edit['priority'] == 'high')
	{ 
	echo 'selected = "selected"'; }
	}
	echo set_select('priority','high');
	?> >High</option>    
    </select>   
    <small class="text-error">
      <?= form_error('priority'); ?>
    </small>
    </td>
    <td></td>
  </tr>
    <?php if(!isset($edit)) {?>
  <tr>
    <td>Subtask</td>
    <td>
    <input type="text" name="subtask[0]" /><small>&nbsp;&nbsp;<a id="new_row" style="cursor:pointer" href="#" ><i class="icon-plus-sign"> </i> Add New Row</a>&nbsp;</small>
    <div id="rows"></div>
    </td>
    <td>&nbsp;</td>
  </tr>
  <?php }?>
  <tr>
    <td><input type="hidden" value="<?= isset($edit['id']) ? $edit['id'] : set_value('edit_id') ?>" name="edit_id" id="edit_id" />
    <input type="hidden" value="<?= site_url('user/tasks/edit')?>" id="edit_url"  />
    <input type="hidden" value="<?= $this->uri->segment(3);?>" id="segment" />
    </td>
    <td><input type="submit" value="<?= isset($edit) ? 'Edit' : 'Create'?>Task" class="btn btn-info" />
    <input type="reset" class="btn btn-danger"/></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<?= form_close()?>
<script type="text/javascript">
$(document).ready(function(e) {
    var edit_id = $("#edit_id").val();
	 var edit_url = $("#edit_url").val();
	var segment = $("#segment").val();
	if(segment == 'create_task' && edit_id ){
	//alert(edit_id)
	var url = edit_url+'/'+edit_id;	
	$(location).attr('href',url);
	}
	
	//$('#dpd1').val(<?= date('y-m-d')?>)
	
	if($('#dpd1').val()=='' &&  $('#dpd2').val()=='' )
	{
	$('#dpd1').val('<?= date("Y-m-d")?>')
	$('#dpd2').val('<?= date("Y-m-d")?>')
	}
	
});
/*var counter = 0;
$("#new_row").click(function () {
	//
	//Create an input type dynamically.
	counter ++;	
    var element = $(document.createElement("input"));
	element.attr('type','text');
	element.attr("name","subtask["+counter+"]");	
	$('#rows').append(element);	
	$('#rows').append('<i class="icon-minus-sign" id="minus"></i>');
	$('#rows').append("<br />");
	//foo.appendChild(element);
 
	})*/
	
	
	
	$("#project").change(function(){
		//alert('dasaas');
		var project_id = $("#project").val();
 $.post('<?= site_url('user/tasks/get_milestone_by_project_id');?>',{project_id : project_id}, function(data) {	
	 //alert(data);
	  $("#milestone").html("")
	 $("#milestone").html(data)
	 });
// get all the task group of related project	 
 $.post('<?= site_url('user/tasks/get_taskgroup_by_project_id');?>',{project_id : project_id}, function(data) {	
 $("#taskgroup").html(data)
 });
 
  $.post('<?= site_url('user/tasks/get_project_manager');?>',{project_id : project_id}, function(data) {	
 $("#user").val(data);
 });
 	 	
		})
	
	/*$("#milestone").change(function(){
		var milestone_id = $("#milestone1").val();
 $.post('<?= site_url('user/tasks/get_taskgroup_by_project_id');?>',{project_id : project_id}, function(data) {	
	 $("#milestone").html(data)
	 });	
		})*/
</script>

<script type="text/javascript">
    var scntDiv = $('#rows');
        var i = $('#rows p').size() + 1;
        
        $('#new_row').live('click', function() {
                $('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="subtask[' + i +']" value="" placeholder="Input Value" />  <a href="#" id="remScnt"> <i class="icon-minus-sign" id="minus"></i></a> </label></p>').appendTo(scntDiv);
                i++;
                return false;
        });
        
        jQuery('#remScnt').live('click', function() { 
		
                if( i > 1 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
			//	$(this).parents("tr").remove();
</script>
